package kr.pe.okjsp.rss;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Iterator;
import java.util.List;

import junit.framework.TestCase;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.xml.sax.InputSource;

public class RSSProxyTest extends TestCase {
	String urlPath = "http://openapi.naver.com/search?key=075f6cf50551a3e6f1d2e9bd88ec9f4b&query=%EB%85%B8%ED%8A%B8%EB%B6%81&display=5&start=1&target=shop&sort=sim";
	public void testGetXml() throws IOException {
		String charset = "utf-8";
		String xml = RSSProxy.getXML(urlPath, charset);
		System.out.println(xml);
	}
	
	public void testJDOM() throws IOException, JDOMException {
		  SAXBuilder parser = new SAXBuilder();
		  parser.setValidation(false);
		  parser.setIgnoringElementContentWhitespace(true);
		  URL url = new URL("http://openapi.naver.com/search?key=075f6cf50551a3e6f1d2e9bd88ec9f4b&query=%EB%85%B8%ED%8A%B8%EB%B6%81&display=5&start=1&target=shop&sort=sim");
		  InputSource is = new InputSource(url.openStream());
		  Document doc = parser.build(is);   

		  Element root = doc.getRootElement(); 
		  Element channel = root.getChild("channel");  
		  
		  List list = channel.getChildren("item"); // List Object Operation
		  for (Iterator iterator = list.iterator(); iterator.hasNext();) {
			  Element object = (Element) iterator.next();
			  String childText = object.getChildText("title");
			  System.out.println(childText);
		}
	}
}
